﻿using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;
using System.Data.Services.Providers;
using PhotoService;

namespace PhotoService
{
    public partial class PhotoData : DataService<PhotoDataContainer>, IServiceProvider
    {
        //private PhotoDataContainer context;

        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
            config.SetEntitySetAccessRule("PhotoInfos",
                EntitySetRights.ReadMultiple |
                EntitySetRights.ReadSingle |
                EntitySetRights.AllWrite);

            config.SetServiceOperationAccessRule("GetTodayPhoto", ServiceOperationRights.AllRead);
            config.SetServiceOperationAccessRule("Like", ServiceOperationRights.AllRead);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        }

        #region IServiceProvider Members
        public object GetService(Type serviceType)
        {
            if (serviceType == typeof(IDataServiceStreamProvider))
            {
                // Return the stream provider to the data service.
                return new PhotoServiceStreamProvider(this.CurrentDataSource);
            }
            return null;
        }
        #endregion

      //  [WebInvoke(Method = "POST")]
        [WebGet]
        public void Like(int id)
        {
           PhotoDataContainer context = this.CurrentDataSource;
           PhotoInfo photo = context.PhotoInfos.Single(p => p.PhotoId == id);
           if (photo != null)
           {
               photo.Comments = "Dislike";
               context.SaveChanges();
           }
        }

        [WebGet]
        public IQueryable<PhotoInfo> GetTodayPhoto()
        {
            PhotoDataContainer context = this.CurrentDataSource;
            return context.PhotoInfos.Take(5);
        }

        [WebInvoke]
        public void Poke(int id)
        {
            int i = id;
        }
    }
    
}
